<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${daoPackage}.${entityName}Mapper">

    <!-- 字段映射  -->
    <resultMap id="BaseResultMap" type="${entityPackage}.${entityName}">
        <#list entityColumns as col>
            <#if col.fieldName == tableId>
                <id column="${col.fieldDbName}" property="${col.fieldName}" jdbcType="${col.myBatisType}"/>
            </#if>
        </#list>
        <#list entityColumns as col>
            <#if col.fieldName != tableId>
                <result column="${col.fieldDbName}" property="${col.fieldName}" jdbcType="${col.myBatisType}"/>
            </#if>
        </#list>
    </resultMap>

    <!-- 共用sql -->
    <sql id="Base_Column_List">
        ${dbnameWithTableId}
    </sql>

    <!-- 共用where sql -->
    <sql id="Where_Column_List">
        <#list entityColumns as col>
                <#if col.fieldName != 'id' && col.fieldName != 'isDeleted' && col.fieldName != 'gmtCreated'>
                     <#if col.dbNameType == 'VARCHAR' >
                         <if test='obj.${col.fieldName} != null and obj.${col.fieldName} != "" '>
                             and ${col.fieldDbName} = ${mybatisOpen}obj.${col.fieldName}}
                         </if>
                     <#else>
                         <if test="obj.${col.fieldName} != null">
                             and ${col.fieldDbName} = ${mybatisOpen}obj.${col.fieldName}}
                         </if>
                     </#if>
                </#if>
        </#list>
    </sql>

    <!-- 查询 -->
    <select id="getById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
        where is_deleted = 'N' and id = ${r'#{id}'}
    </select>

    <select id="countByPara" resultType="java.lang.Integer">
        select
        count(*)
        from ${tableName}
        where is_deleted = 'N'
        <include refid="Where_Column_List"/>
    </select>

    <!-- 分页查询 -->
    <select id="pageByPara" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
        where is_deleted='N'
        <include refid="Where_Column_List"/>
        <include refid="Public.OrderCriteria"/>
        <include refid="Public.afterPage"/>
    </select>

    <!-- 列表查询 -->
    <select id="listByPara" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${tableName}
        where is_deleted='N'
        <include refid="Where_Column_List"/>
        <include refid="Public.OrderCriteria"/>
    </select>

    <!-- 插入操作 -->
    <insert id="insert" parameterType="${entityPackage}.${entityName}" useGeneratedKeys="true" keyProperty="id">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list entityColumns as col>
                <if test="${col.fieldName} != null">
                    <#if entityColumns?size - 1 == col_index>
                        ${col.fieldDbName}
                    <#else>
                        ${col.fieldDbName},
                    </#if>
                </if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list entityColumns as col>
                <if test="${col.fieldName} != null">
                    <#if entityColumns?size - 1 == col_index>
                        ${col.insertUpdateMyBatisField}
                    <#else>
                        ${col.insertUpdateMyBatisField},
                    </#if>
                </if>
            </#list>
        </trim>
    </insert>


    <!-- 更新 -->
    <update id="updateById">
        update ${tableName}
        <set>
            <#list entityColumns as col>
                <#if entityColumns?size - 1 == col_index>
                    <#if col.fieldName != 'id' && col.fieldName != 'creator' && col.fieldName != 'gmtCreated'>
                        <if test="obj.${col.fieldName} != null">
                            ${col.fieldDbName} = ${mybatisOpen}obj.${col.fieldName}
                            ,jdbcType=${col.dbNameType}${mybatisClose}
                        </if>
                    </#if>
                <#else>
                    <#if col.fieldName != 'id' && col.fieldName != 'creator' && col.fieldName != 'gmtCreated'>
                        <if test="obj.${col.fieldName} != null">
                            ${col.fieldDbName} = ${mybatisOpen}obj.${col.fieldName}
                            ,jdbcType=${col.dbNameType}${mybatisClose},
                        </if>
                    </#if>
                </#if>
            </#list>
        </set>
        where is_deleted = 'N' and id=${r'#{obj.id}'}
    </update>
</mapper>